﻿using System;
using System.IO;
using NVelocity;
using NVelocity.App;
using Commons.Collections;
using NVelocity.Context;

namespace HiTeachers.Utilities
{
    public class NVelocityBuilder
    {
        private static string _resourcePath;
        public static void SetResourcePath(string resPath)
        {
            _resourcePath = resPath;
        }

        private static string GetTemplateContent(string tmpFile)
        {
            //StreamReader stReader = File.OpenText(tmpFile);
            //return stReader.ReadToEnd();
            return tmpFile;
        }

        public static string ContactBuilder(string tempFile, string cntName, string cntEmail, string cntContent)
        {
            try
            {
                //var velocity = new VelocityEngine();
                //var props = new ExtendedProperties();
                //props.AddProperty("file.resource.loader.path", _resourcePath);
                //velocity.Init(props);

                //Template template = velocity.GetTemplate(tempFile);

                //var context = new VelocityContext();

                string mailTemplate = GetTemplateContent(tempFile);
                mailTemplate = mailTemplate.Replace("$contactName", cntName);
                mailTemplate = mailTemplate.Replace("$contactEmail", cntEmail);
                mailTemplate = mailTemplate.Replace("$contactContent", cntContent);

                //context.Put("contactName", cntName);
                //context.Put("contactEmail", cntEmail);
                //context.Put("contactContent", cntContent);

                //var writer = new StringWriter();
                //template.Merge(context, writer);
                //return writer.GetStringBuilder().ToString();
                return mailTemplate;
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return string.Empty;
            }
        }

        public static string VerificationEmailBuilder(string tempFile, string siteUrl, Guid requestId, string studentName)
        {
            try
            {
                //var velocity = new VelocityEngine();
                //var props = new ExtendedProperties();
                //props.AddProperty("file.resource.loader.path", _resourcePath);
                //velocity.Init(props);

                //Template template = velocity.GetTemplate(tempFile);

                //var context = new VelocityContext();

                string mailTemplate = GetTemplateContent(tempFile);
                mailTemplate = mailTemplate.Replace("$studentName", studentName);
                mailTemplate = mailTemplate.Replace("$urlActivate", string.Format("{0}/verification.aspx?rqpid={1}", siteUrl, requestId));

                //var writer = new StringWriter();
                //template.Merge(context, writer);
                //return writer.GetStringBuilder().ToString();
                return mailTemplate;
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return string.Empty;
            }
        }

        public static string Contact2StudentEmailBuilder(string tempFile, string studentName, string urlToTeacher, string contactText)
        {
            try
            {
                //var velocity = new VelocityEngine();
                //var props = new ExtendedProperties();
                //props.AddProperty("file.resource.loader.path", _resourcePath);
                //velocity.Init(props);

                //Template template = velocity.GetTemplate(tempFile);

                //var context = new VelocityContext();

                string mailTemplate = GetTemplateContent(tempFile);
                mailTemplate = mailTemplate.Replace("$studentName", studentName);
                mailTemplate = mailTemplate.Replace("$urlToTeacher", urlToTeacher);
                mailTemplate = mailTemplate.Replace("$contactText", contactText);

                //var writer = new StringWriter();
                //template.Merge(context, writer);
                //return writer.GetStringBuilder().ToString();
                return mailTemplate;
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return string.Empty;
            }
        }

        public static string ActivationAccountEmailBuilder(string tempFile, string teacherName, string urlActivate, string teacherEmail, string teacherPassword)
        {
            try
            {
                //var velocity = new VelocityEngine();
                //var props = new ExtendedProperties();
                //props.AddProperty("file.resource.loader.path", _resourcePath);
                //velocity.Init(props);

                //Template template = velocity.GetTemplate(tempFile);

                //var context = new VelocityContext();

                string mailTemplate = GetTemplateContent(tempFile);
                mailTemplate = mailTemplate.Replace("$teacherName", teacherName);
                mailTemplate = mailTemplate.Replace("$urlActivate", urlActivate);
                mailTemplate = mailTemplate.Replace("$teacherEmail", teacherEmail);
                mailTemplate = mailTemplate.Replace("$teacherPassword", teacherPassword);

                //var writer = new StringWriter();
                //template.Merge(context, writer);
                //return writer.GetStringBuilder().ToString();
                return mailTemplate;
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return string.Empty;
            }
        }

        public static string ConfirmSubcribeEmailBuilder(string tempFile, string subcribeEmal, string confirmSubcribeUrl, string unSubcribeUrl)
        {
            try
            {
                //var velocity = new VelocityEngine();
                //var props = new ExtendedProperties();
                //props.AddProperty("file.resource.loader.path", _resourcePath);
                //velocity.Init(props);

                //Template template = velocity.GetTemplate(tempFile);

                //var context = new VelocityContext();

                string mailTemplate = GetTemplateContent(tempFile);
                mailTemplate = mailTemplate.Replace("$subscribeEmail", subcribeEmal);
                mailTemplate = mailTemplate.Replace("$confirmSubscribeUrl", confirmSubcribeUrl);
                mailTemplate = mailTemplate.Replace("$unSubscribeUrl", unSubcribeUrl);

                //var writer = new StringWriter();
                //template.Merge(context, writer);
                //return writer.GetStringBuilder().ToString();
                return mailTemplate;
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return string.Empty;
            }
        }

        public static string PwChangedNotificationEmailBuilder(string tempFile, string teacherName, string userName, string newPassword)
        {
            try
            {
                //var velocity = new VelocityEngine();
                //var props = new ExtendedProperties();
                //props.AddProperty("file.resource.loader.path", _resourcePath);
                //velocity.Init(props);

                //Template template = velocity.GetTemplate(tempFile);

                //var context = new VelocityContext();

                string mailTemplate = GetTemplateContent(tempFile);
                mailTemplate = mailTemplate.Replace("$teacherName", teacherName);
                mailTemplate = mailTemplate.Replace("$usernameName", userName);
                mailTemplate = mailTemplate.Replace("$teacherPassword", newPassword);

                //var writer = new StringWriter();
                //template.Merge(context, writer);
                //return writer.GetStringBuilder().ToString();
                return mailTemplate;
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return string.Empty;
            }
        }

        public static string PostReviewedEmailBuilder(string tempFile, string studentName)
        {
            try
            {
                //var velocity = new VelocityEngine();
                //var props = new ExtendedProperties();
                //props.AddProperty("file.resource.loader.path", _resourcePath);
                //velocity.Init(props);

                //Template template = velocity.GetTemplate(tempFile);

                //var context = new VelocityContext();

                string mailTemplate = GetTemplateContent(tempFile);
                mailTemplate = mailTemplate.Replace("$studentName", studentName);

                //var writer = new StringWriter();
                //template.Merge(context, writer);
                //return writer.GetStringBuilder().ToString();
                return mailTemplate;
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return string.Empty;
            }
        }

        public static string RemindEmailBuilder(string tempFile, string teacherName, string teacherEmail, string techPassword,
            DateTime actFromDate, DateTime actToDate)
        {
            try
            {
                //var velocity = new VelocityEngine();
                //var props = new ExtendedProperties();
                //props.AddProperty("file.resource.loader.path", _resourcePath);
                //velocity.Init(props);

                //Template template = velocity.GetTemplate(tempFile);

                //var context = new VelocityContext();

                string mailTemplate = GetTemplateContent(tempFile);
                mailTemplate = mailTemplate.Replace("$teacherName", teacherName);
                mailTemplate = mailTemplate.Replace("$teacherEmail", teacherEmail);
                mailTemplate = mailTemplate.Replace("$teachPassword", techPassword);
                mailTemplate = mailTemplate.Replace("$atcFromDate", actFromDate.ToString("dd/MM/yyyy"));
                mailTemplate = mailTemplate.Replace("$atcToDate", actToDate.ToString("dd/MM/yyyy"));

                //var writer = new StringWriter();
                //template.Merge(context, writer);
                //return writer.GetStringBuilder().ToString();
                return mailTemplate;
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return string.Empty;
            }
        }

        public static string ExpiredEmailBuilder(string tempFile, string teacherName, string teacherEmail, string techPassword)
        {
            try
            {
                //var velocity = new VelocityEngine();
                //var props = new ExtendedProperties();
                //props.AddProperty("file.resource.loader.path", _resourcePath);
                //velocity.Init(props);

                //Template template = velocity.GetTemplate(tempFile);

                //var context = new VelocityContext();

                string mailTemplate = GetTemplateContent(tempFile);
                mailTemplate = mailTemplate.Replace("$teacherName", teacherName);
                mailTemplate = mailTemplate.Replace("$teacherEmail", teacherEmail);
                mailTemplate = mailTemplate.Replace("$teachPassword", techPassword);

                //var writer = new StringWriter();
                //template.Merge(context, writer);
                //return writer.GetStringBuilder().ToString();
                return mailTemplate;
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return string.Empty;
            }
        }

        public static string PaymentEmailBuider(string tempFile, string teacherName, string teacherEmail, string techPassword, DateTime actFrom,
            DateTime atcTo, string paiedFees, DateTime paiedDate)
        {
            try
            {
                //var velocity = new VelocityEngine();
                //var props = new ExtendedProperties();
                //props.AddProperty("file.resource.loader.path", _resourcePath);
                //velocity.Init(props);

                //Template template = velocity.GetTemplate(tempFile);

                //var context = new VelocityContext();

                string mailTemplate = GetTemplateContent(tempFile);
                mailTemplate = mailTemplate.Replace("$teacherName", teacherName);
                mailTemplate = mailTemplate.Replace("$teacherEmail", teacherEmail);
                mailTemplate = mailTemplate.Replace("$teachPassword", techPassword);
                mailTemplate = mailTemplate.Replace("$atcPeriod", string.Format("Từ {0} đến {1}.", actFrom.ToString("dd/MM/yyyy"), atcTo.ToString("dd/MM/yyyy")));
                mailTemplate = mailTemplate.Replace("$paiedFees", paiedFees);
                mailTemplate = mailTemplate.Replace("$paiedDate", paiedDate.ToString("dd/MM/yyyy"));

                //var writer = new StringWriter();
                //template.Merge(context, writer);
                //return writer.GetStringBuilder().ToString();
                return mailTemplate;
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return string.Empty;
            }
        }

        public static string AcceptContactEmailBuilder(string tempFile, string teacherName, string studentName, string teachSubject,
            string urlPostRequest, string postRequestTitle)
        {
            try
            {
                //var velocity = new VelocityEngine();
                //var props = new ExtendedProperties();
                //props.AddProperty("file.resource.loader.path", _resourcePath);
                //velocity.Init(props);

                //Template template = velocity.GetTemplate(tempFile);

                //var context = new VelocityContext();

                string mailTemplate = GetTemplateContent(tempFile);
                mailTemplate = mailTemplate.Replace("$teacherName", teacherName);
                mailTemplate = mailTemplate.Replace("$studentName", studentName);
                mailTemplate = mailTemplate.Replace("$teachSubject", teachSubject);
                mailTemplate = mailTemplate.Replace("$urlPostRequest", urlPostRequest);
                mailTemplate = mailTemplate.Replace("$postRequestTitle", postRequestTitle);

                //var writer = new StringWriter();
                //template.Merge(context, writer);
                //return writer.GetStringBuilder().ToString();
                return mailTemplate;
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return string.Empty;
            }
        }

        public static string AcceptedStudentEmailBuider(string tempFile, string studentName, string teacherName, string contactInfo,
            string teacherAddress, string feedbackLink)
        {
            try
            {
                //var velocity = new VelocityEngine();
                //var props = new ExtendedProperties();
                //props.AddProperty("file.resource.loader.path", _resourcePath);
                //velocity.Init(props);

                //Template template = velocity.GetTemplate(tempFile);

                //var context = new VelocityContext();

                string mailTemplate = GetTemplateContent(tempFile);
                mailTemplate = mailTemplate.Replace("$studentName", studentName);
                mailTemplate = mailTemplate.Replace("$teacherName", teacherName);
                mailTemplate = mailTemplate.Replace("$contactInfo", contactInfo);
                mailTemplate = mailTemplate.Replace("$teacherAddress", teacherAddress);
                mailTemplate = mailTemplate.Replace("$feedbackLink", feedbackLink);

                //var writer = new StringWriter();
                //template.Merge(context, writer);
                //return writer.GetStringBuilder().ToString();
                return mailTemplate;
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return string.Empty;
            }
        }

        public static string RefuseContactEmailBuilder(string tempFile, string refuseTeacherName, string studentName, string aptTeacherName,
            string urlPostRequest, string postRequestTitle)
        {
            try
            {
                //var velocity = new VelocityEngine();
                //var props = new ExtendedProperties();
                //props.AddProperty("file.resource.loader.path", _resourcePath);
                //velocity.Init(props);

                //Template template = velocity.GetTemplate(tempFile);

                //var context = new VelocityContext();

                string mailTemplate = GetTemplateContent(tempFile);
                mailTemplate = mailTemplate.Replace("$refuseTeacherName", refuseTeacherName);
                mailTemplate = mailTemplate.Replace("$studentName", studentName);
                mailTemplate = mailTemplate.Replace("$acceptedTeacherName", aptTeacherName);
                mailTemplate = mailTemplate.Replace("$urlPostRequest", urlPostRequest);
                mailTemplate = mailTemplate.Replace("$postRequestTitle", postRequestTitle);

                //var writer = new StringWriter();
                //template.Merge(context, writer);
                //return writer.GetStringBuilder().ToString();
                return mailTemplate;
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return string.Empty;
            }
        }

        public static string ForgetPasswordEmailBuilder(string tempFile, string teacherName, string userName, string newPassword)
        {
            try
            {
                //var velocity = new VelocityEngine();
                //var props = new ExtendedProperties();
                //props.AddProperty("file.resource.loader.path", _resourcePath);
                //velocity.Init(props);

                //Template template = velocity.GetTemplate(tempFile);

                //var context = new VelocityContext();

                string mailTemplate = GetTemplateContent(tempFile);
                mailTemplate = mailTemplate.Replace("$teacherName", teacherName);
                mailTemplate = mailTemplate.Replace("$usernameName", userName);
                mailTemplate = mailTemplate.Replace("$teacherPassword", newPassword);

                //var writer = new StringWriter();
                //template.Merge(context, writer);
                //return writer.GetStringBuilder().ToString();
                return mailTemplate;
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return string.Empty;
            }
        }
    }
}
